package cn.kgc.springmvc02.controller;

import cn.kgc.springmvc02.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: mosin
 * @version: v1.0  2022/12/12
 * springmvc中页面跳转的范式
 * 1.转发  默认返回字符串 就是转发的方式  经过视图解析器
 *        forward 添加了 forward关键词的字符串  不在经过视图解析器
 * 2.重定向
 *      redirect：添加了 redirect关键词的字符串  不在经过视图解析器
 */
@Controller
@RequestMapping("rf")
public class RFController {

    @RequestMapping("f1")
    public String  f1(){
        System.out.println("请求转发到index.jsp");
        return "index";  //  转发  经过视图解析器
    }

    @RequestMapping("f2")
    public String  f2(){
        System.out.println("请求转发到forward：index.jsp");
        return "forward:/index.jsp";  //  转发  不经过视图解析器
    }

    @RequestMapping("f3")
    public void  f3(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("通过request对象将请求转发到index.jsp");
        request.getRequestDispatcher("/index.jsp").forward(request, response);
    }

    @RequestMapping("f4")
    public void  f4(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("通过request对象将请求转发到index.jsp");
        request.setAttribute("username", "tom");
        request.getRequestDispatcher("/index.jsp").forward(request, response);
    }

    @RequestMapping("f5")
    public String  f5(Model model){
        System.out.println("请求转发到index.jsp");
        //存入model对象中的值  会被放入request作用域
        model.addAttribute("username", "xiaohei");
        return "index";  //  转发  经过视图解析器
    }


    @RequestMapping("f6")
    public ModelAndView f5(ModelAndView modelAndView){
        System.out.println("请求转发到index.jsp");
        //设置视图名
        modelAndView.setViewName("index");
        modelAndView.addObject("username", "xiaoming");
        return  modelAndView;  // 转发  经过视图解析器
    }


    @RequestMapping("r1")
    public String  r1(){
        System.out.println("请求重定向到index.jsp");
        return "redirect:/index.jsp";  //  重定向  不经过视图解析器
    }


    @RequestMapping("r2")
    public void  r2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("通过response对象将请求重定向到index.jsp");
        HttpSession session = request.getSession();
        session.setAttribute("username", "jack");
        response.sendRedirect(request.getContextPath()+"/index.jsp");
    }


    @RequestMapping("r3")
    public String  r3(HttpSession session){
        System.out.println("请求重定向到index.jsp");
        session.setAttribute("username", "lucy");
        return "redirect:/index.jsp";  //  重定向  不经过视图解析器
    }

    @RequestMapping("json")
    @ResponseBody
    public User method(){
        User user = new User();
        user.setAge(20);
        user.setUsername("tom");
        return user;
    }

    @RequestMapping("json2")
    @ResponseBody
    public List<User>  method2(){
        ArrayList<User> users = new ArrayList<>();
        User user = new User();
        user.setAge(20);
        user.setUsername("tom");
        users.add(user);
        users.add(user);
        users.add(user);
        return users;
    }


}
